#include "user_sign.h"
#include <mysql/mysql.h>
#include <header.h>
//typedef struct user_info_s{
//    char user_name[32];
//    char salt[8];
//    char cryptpasswd[16];
//    char pwd[64];
//}user_info_t;
//


//  注册一个用户名，返回值为0：注册成功     为1：注册失败
int Register(user_info_s *reg_user){        
    MYSQL * mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql,"localhost","root",reg_user->cryptpasswd,"tuser_info",0,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"mysql_real_connect:%s\n", mysql_error(mysql));
        return -1;
    }

    //  测试数据库已注册的用户
    // char    sql[1024] = "select * from tuser_info;";
   // int qret = mysql_query(mysql,sql);
   // if(qret!=0){
   //     fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
   //     return -1;
   // }

   // MYSQL_RES *table = mysql_store_result(mysql);
   // printf("col = %u,row %lu\n",mysql_num_fields(table),mysql_num_rows(table));
   // for(unsigned long i=0;i<mysql_num_rows(table);i++){
   //     MYSQL_ROW row = mysql_fetch_row(table);
   //     for(unsigned int j=0;j<mysql_num_fields(table);j++){
   //         printf("%s\t",row[j]);
   //     }
   //     printf("\n");
   // }
    //  -----------------------------------------  判断是否重名  ------------------------------------------------   //
    /* char sql[1024] = "select * from tuser_info where username = ' "; */
    /* strcat(sql,reg_user->user_name); */
    /* strcat(sql,"';"); */
    char sql[1024];
    sprintf(sql,"select * from tuser_info where username = '%s';",reg_user->user_name);
    //  满足sql中的内容为“select * from tuser_info where username = 'reg_user->user_name'”;
    int qret = mysql_query(mysql,sql);
    if(qret == 0){
        printf("用户表中没有目标用户名，可进行注册！");
    }
    else{
        printf("该用户名已注册！");
        return -1;
    }
    mysql_store_result(mysql);

    //  -------------------------------------   添加用户信息到注册表    -----------------------------------------   //
    bzero(sql,sizeof(sql));
    sprintf(sql,"insert into tuser_info (username,salt,cryptpasswd,pwd) values (%s,%s,%s,%s)",reg_user->user_name,reg_user->salt,reg_user->cryptpasswd,reg_user->pwd);
    qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query 1:%s\n",mysql_error(mysql));
        return -1;
    }
    
    mysql_close(mysql);
    return 0;
}

//  根据sign_user中的数据和数据表匹配   -1：匹配失败     0：匹配成功，登入
int Sign_in(const user_info_s *sign_user){
    MYSQL * mysql = mysql_init(NULL);
    MYSQL * ret = mysql_real_connect(mysql,"localhost","root",sign_user->cryptpasswd,"tuser_info",0,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"mysql_real_connect:%s\n",mysql_error(mysql));
        return -1;   
    }

    //  ---------------------------------   提前输入用户名行    ---------------------------------------------   //
    char sql[1024];
    sprintf(sql,"select * from tuser_info where username = '%s';",sign_user->user_name);
    int qret = mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
        return -1;
    }

    MYSQL_RES * table = mysql_store_result(mysql);
    printf("col = %u, row = %lu\n", mysql_num_fields(table), mysql_num_rows(table));
    MYSQL_ROW row = mysql_fetch_row(table);
    int flag = 1;
    if(strcpy(row[2],sign_user->salt)!=0){
        flag = 0;
    }
    if(strcpy(row[2],sign_user->salt)!=0){
        flag = 0;
    }

    if(flag){
        printf("用户信息匹配，成功登录！");
    }
    else{
        printf("用户名/密码错误，请重新输入！");
        return -1;
    }

    return 0;
}


